package unclassified.medium;

public class WateringPlantsII_5954 {

    public int minimumRefill(int[] plants, int capacityA, int capacityB) {
        int l = 0, r = plants.length - 1;
        int curA = capacityA, curB = capacityB;
        int res = 0;
        while (l < r) {
            if (curA < plants[l]) {
                res++;
                curA = capacityA;
            }
            if (curB < plants[r]) {
                res++;
                curB = capacityB;
            }
            curA -= plants[l++];
            curB -= plants[r--];
        }
        if (l == r) {
            if (curA < plants[l] && curB < plants[r]) {
                res++;
            }
        }
        return res;
    }
}
